import { Module } from 'vuex'
import type { IsystemState } from './type'
import type { IRootState } from '../types'
import {
  getPageListData,
  deletePageData,
  createPageData,
  editPageData
} from '../../service/main/system/system'
const systemModule: Module<IsystemState, IRootState> = {
  namespaced: true,
  state() {
    return {
      usersList: [],
      usersCount: 0,
      roleList: [],
      roleCount: 0,
      goodsList: [],
      goodsCount: 0,
      menuList: [],
      menuCount: 0,
      departmentList: [],
      departmentCount: 0,
      categoryList: [],
      categoryCount: 0,
      storyList: [],
      storyCount: 0
    }
  },
  mutations: {
    changeUsersList(state, userList: any[]) {
      state.usersList = userList
    },
    changeUsersCount(state, userCount: number) {
      state.usersCount = userCount
    },
    changeRoleList(state, roleList: any[]) {
      state.roleList = roleList
    },
    changeRoleCount(state, roleCount: number) {
      state.roleCount = roleCount
    },
    changeGoodsList(state, goodsList: any[]) {
      state.goodsList = goodsList
    },
    changeGoodsCount(state, goodsCount: number) {
      state.goodsCount = goodsCount
    },
    changeMenuList(state, menuList: any[]) {
      state.menuList = menuList
    },
    changeMenuCount(state, menuCount: number) {
      state.menuCount = menuCount
    },
    changeDepartmentList(state, deparmentList: any[]) {
      state.departmentList = deparmentList
    },
    changeDepartmentCount(state, deparmentCount: number) {
      state.departmentCount = deparmentCount
    },
    changeCategoryList(state, categoryList: any[]) {
      state.categoryList = categoryList
    },
    changeCategoryCount(state, categoryCount: number) {
      state.categoryCount = categoryCount
    },
    changeStoryList(state, storyList: any[]) {
      state.storyList = storyList
    },
    changeStoryCount(state, storyCount: number) {
      state.storyCount = storyCount
    }
  },
  getters: {
    getListData(state) {
      return (pageName: string) => {
        return (state as any)[`${pageName}List`]
      }
    },
    getListCount(state) {
      return (pageName: string) => {
        return (state as any)[`${pageName}Count`]
      }
    }
  },
  actions: {
    async getPageListAction({ commit }, payload: any) {
      const pageName = payload.pageName
      const pageUrl = `/${pageName}/list`

      // switch (pageName) {
      //   case 'users':
      //     pageUrl = '/users/list'
      //     break
      //   case 'role':
      //     pageUrl = '/role/list'
      //     break
      // }
      // console.log(pageUrl)
      const pageResult = await getPageListData(pageUrl, payload.queryInfo)
      const { list, totalCount } = pageResult.data

      const pagePageName =
        pageName.slice(0, 1).toUpperCase() + pageName.slice(1)
      //console.log(pagePageName)
      commit(`change${pagePageName}List`, list)
      commit(`change${pagePageName}Count`, totalCount)

      // if (pageUrl === 'users') {
      //   commit('changeUsersList', list)
      //   commit('changeUsersCount', totalCount)
      // } else if (pageUrl === 'role') {
      //   commit('changeRoleList', list)
      //   commit('changeRoleCount', totalCount)
      // }
    },

    async deletePageList({ dispatch }, payload: any) {
      const { pageName, id } = payload
      const pageUrl = `/${pageName}/${id}`
      await deletePageData(pageUrl)

      //重新请求最新数据
      dispatch('getPageListAction', {
        pageName: pageName,
        queryInfo: {
          offset: 1,
          size: 10
        }
      })
    },

    async createPageAction({ dispatch }, payload: any) {
      const { pageName, newData } = payload
      const pageUrl = `/${pageName}`
      await createPageData(pageUrl, newData)
      dispatch('getPageListAction', {
        pageName,
        queryInfo: {
          offset: 1,
          size: 10
        }
      })
    },

    async pathPageAction({ dispatch }, payload: any) {
      const { pageName, editData, id } = payload
      const pageUrl = `/${pageName}/${id}`
      await editPageData(pageUrl, editData)

      dispatch('getPageListAction', {
        pageName,
        queryInfo: {
          offset: 1,
          size: 10
        }
      })
    }
  }
}

export default systemModule
